package com.geekbang.week5;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

/**
 * Filename:    com.geekbang.week5.ServiceHash
 * Copyright:   Copyright (c) 2020

 *
 * @author 于泳
 * @version: 1.0
 * @since: JDK 1.8
 * Create at:   2020/11/22 16:52
 * Description:
 * <p>
 * </p>
 * Modification History:
 * Date    Author      Version     Description
 * -----------------------------------------------------------------
 * 2020/11/22 16:52 于泳      1.0     1.0 Version
 */
public class ServiceHash {

//    node 服务器ip建立虚拟节点，存储虚拟节点对应实际服务器ip hash表
    private static Map<String,String> NODE_MAP_SERVER_IP = new ConcurrentHashMap<>();
//    虚拟ip hash 值映射
//    private static Map<String,String> NODE_MAP_SERVER_IP_WZ = new ConcurrentHashMap<>();
//    每个服务器节点，拆分成虚拟节点数量
    private static  int SPLIT_NUM = 200;

    private static String[] HASH_RING = new String[0x7fffffff];



    public void add(String serviceIp){

        List<String> vIpList = splitService(serviceIp);
        for(String vIp:vIpList){
            HASH_RING[hashCode(vIp)]=vIp;
        }

    }

//  服务器ip拆分成虚拟ip列表
    private List<String> splitService(String serviceIp){
        List<String> list = new ArrayList<>();
        for(int i=0;i<SPLIT_NUM;i++){

            String vIp = serviceIp+"_"+i;
            NODE_MAP_SERVER_IP.put(vIp,serviceIp);
            list.add(vIp);
        }
        return list;
    }

    private int  hashCode(String vIp){

        return 0;
    }


    public String  getService(Object key){

        String vIp = "";
        return NODE_MAP_SERVER_IP.get(vIp);
    }


}
